package com.junqson.je.user.service;

import com.junqson.je.common.model.ApiResult;
import com.junqson.je.user.model.UserConcernModel;
import com.junqson.je.user.repo.UserConcernRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/3 17:32
 * @Version 1.0
 */
@Service
public class UserConcernService {
    @Autowired
    UserConcernRepo userConcernRepo;


    /**
     * 获取关注的用户列表
     * @param userId
     * @return
     */
    public List<Integer> getConcernUsers(Integer userId) {
        List<UserConcernModel> concerns = userConcernRepo.findByUserId(userId);

        List<Integer> userIds = concerns.stream().map(UserConcernModel::getConcernUserId)
                .collect(Collectors.toList());
        return userIds;
    }


    /**
     * 获取粉丝列表
     */
    public List<Integer> getConcernBy(Integer userId) {
        List<UserConcernModel> funs = userConcernRepo.findByConcernUserId(userId);
        return funs.stream().map(UserConcernModel::getUserId)
                .collect(Collectors.toList());
    }


    /**
     * 关注或取消关注
     */
    public ApiResult concernOrCancel(Integer userId, Integer concernId) {
        UserConcernModel model = userConcernRepo.findByUserIdAndConcernUserId(userId, concernId);
        // 已关注则取消
        if (model != null) {
            userConcernRepo.delete(model);
            return ApiResult.success().setMsg("取消关注成功");
        }
        model = new UserConcernModel();
        model.setUserId(userId);
        model.setConcernUserId(concernId);
        userConcernRepo.save(model);
        return ApiResult.success().setMsg("关注成功");

    }


}
